﻿// 讀取網頁資料
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Drawing;

namespace SDD
{
    class ReadWebPage
    {
        public List<string> pageUrls = new List<string>();

        public void Read()
        {
            pageUrls.Clear();
            
            string rootUrl = "http://sirobrother.blogspot.tw/";
            WebBrowser webBrowser1 = new WebBrowser();
            webBrowser1.NewWindow += new CancelEventHandler(webBrowser1_NewWindow);
            webBrowser1.Navigate(rootUrl);
            webBrowser1.ScriptErrorsSuppressed = true;

            //而Application.DoEvents();是讓程式在跑迴圈時還能去傾聽其他的事件
            while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
                System.Windows.Forms.Application.DoEvents();
            //Console.WriteLine("done");

            //這邊使用的是一種叫做DOM的技術，DOM是Document Object Model 
            HtmlDocument doc = webBrowser1.Document;
            //取得所有超聯結
            HtmlElementCollection Ha = doc.GetElementsByTagName("a");
            foreach (HtmlElement aa in Ha)
            {
                //取得文章有效的連結
                string urltmp = aa.GetAttribute("href").Replace("about:", "");
                if (urltmp.Contains(rootUrl + "20") &&
                    !(urltmp.Contains("#")) &&
                    new Uri(urltmp).Segments.Length > 3 &&
                    urltmp.Replace(rootUrl + "20", "").Length > 1 &&
                    !pageUrls.Contains(urltmp))
                {
                    double rdno = new Random().Next(0, 10);
                    //取得文章機率 8成
                    //曝光機率
                    if (rdno > 2)
                        pageUrls.Add(urltmp);
                    //Console.WriteLine(urltmp);
                }
            }
            //文章清單
            foreach (string subUrl in pageUrls)
            {
                webBrowser1.Navigate(subUrl);
                while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
                    System.Windows.Forms.Application.DoEvents();
                //廣告被點擊機率 7成
                double rdno = new Random().Next(0, 10);
                
                System.Threading.Thread.Sleep((int)(rdno * 567));

                if (rdno > 3)
                {
                    //Console.WriteLine(subUrl);
                    ClickPageAD(ref webBrowser1);
                }
            }//文章清單 end

            //結束
            webBrowser1.Dispose();
        }

        /// <summary>
        /// ClickAD
        /// </summary>
        /// <param name="wb"></param>
        public void ClickPageAD(ref WebBrowser wb)
        {
            HtmlElementCollection divs = wb.Document.GetElementsByTagName("div");
            foreach (HtmlElement div in divs)
            {
                if (div.Id == "HTML1")
                {
                    try
                    {
                        string eid = "EMBED_CCCCCCCCCCCCC";
                        div.Document.GetElementsByTagName("EMBED")[0].Id = eid;
                        Object[] objArray = new Object[1];
                        objArray[0] = (Object)eid;
                        wb.Document.InvokeScript("siro000001", objArray);
                        //Console.WriteLine(div.Document.GetElementsByTagName("EMBED")[0].InnerHtml);

                        //似乎不work
                        //wb.Document.InvokeScript();
                        //div.Document.GetElementsByTagName("EMBED")[0];
                        //document.getElementById("button Id").click()

                        //似乎不work
                        //Console.WriteLine(div.Document.GetElementsByTagName("div")[0].InnerHtml);
                        //WebBrowser wbs = new WebBrowser();
                        //wbs.Navigate(div.Document.GetElementsByTagName("EMBED")[0].GetAttribute("src"));
                        //while (wbs.ReadyState != WebBrowserReadyState.Complete)
                        //    System.Windows.Forms.Application.DoEvents();
                    }
                    finally
                    { }
                }
                if (div.Id == "HTML2")
                {
                    try
                    {
                        double rdno = new Random().Next(0, 10);
                        //點第一個廣告機率9成
                        if (rdno > 1 && div.GetElementsByTagName("INS").Count > 2)
                        {
                            System.Threading.Thread.Sleep((int)(rdno * 128));
                            div.GetElementsByTagName("INS")[2].GetElementsByTagName("INS")[3].InvokeMember("click");
                            //Console.WriteLine("click : " + div.GetElementsByTagName("INS")[2].InnerText);
                        }
                        else
                        {
                            //Console.WriteLine("1 unclick  ");
                        }
                        //點第二個廣告機率7成
                        if (rdno > 3 && div.GetElementsByTagName("INS").Count > 8)
                        {
                            System.Threading.Thread.Sleep((int)(rdno * 256));
                            div.GetElementsByTagName("INS")[8].GetElementsByTagName("INS")[3].InvokeMember("click");
                            //Console.WriteLine("click : " + div.GetElementsByTagName("INS")[8].InnerText);
                        }
                        else {
                            //Console.WriteLine("2 unclick  ");
                        }
                        //點第一個廣告機率3成
                        if (rdno > 7 && div.GetElementsByTagName("INS").Count > 14)
                        {
                            System.Threading.Thread.Sleep((int)(rdno * 512));
                            div.GetElementsByTagName("INS")[14].GetElementsByTagName("INS")[3].InvokeMember("click");
                            //Console.WriteLine("click : " + div.GetElementsByTagName("INS")[14].InnerText);
                        }
                        else
                        {
                            //Console.WriteLine("3 unclick  ");
                        }
                        //Console.WriteLine(div.InnerHtml); 
                    }
                    finally
                    { }
                  
                }
            }
        }

        /// <summary>
        /// 這是webBrowser1的"事件"，當跳出新視窗時要做什麼
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            //這行就是讓多跳出的新視窗跳不出來 =.="
            e.Cancel = true;
        }

    }
}